﻿Imports LinhKienMayTinh.CommonMethods
Imports LinhKienMayTinh.CommonConstants

Public Class MasterManagement
    Inherits BasePage

    Protected dsSource As DataSet

    Public Enum MasterGroup
        Category
        Manufactory
    End Enum

    Private Sub MasterManagement_Init(sender As Object, e As EventArgs) Handles Me.Init
        Try
            Dim master As AdminMasterPage = DirectCast(Me.Page.Master, AdminMasterPage)
            master.CssLinkAdd(ResolveUrl("~/Style/MasterManagementStyle.css"))

            master.ScriptLinkAdd(ResolveUrl("~/Scripts/MasterManagement.js"))
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            Try
                GetMaster()
            Catch ex As Exception

            End Try
        End If
    End Sub

    Protected Overridable Sub GetMaster()
        Dim xMasterTb As String
        Dim xSql As String
        Dim dataAccess As DBAccess

        Try
            dataAccess = Me.GetDBAccess()

            If radCategory.Checked Then
                xMasterTb = CommonConstants.T_CATEGORY
            Else
                xMasterTb = CommonConstants.T_MANUFACTORY
            End If

            xSql = "SELECT CODE, NAME FROM {0}"
            xSql = String.Format(xSql, xMasterTb)

            dsSource = dataAccess.GetDBDataset(xSql)
        Catch ex As Exception
        End Try
    End Sub

    Protected Overridable Function DeleteMaster() As Integer
        Dim xSql As String
        Dim xMasterTb As String
        Dim dataAccess As DBAccess

        Try
            If radCategory.Checked Then
                xMasterTb = CommonConstants.T_CATEGORY
            Else
                xMasterTb = CommonConstants.T_MANUFACTORY
            End If

            If xMasterTb = String.Empty Then
                Return 0
            End If

            xSql = "DELETE FROM {0} WHERE CODE = {1}"
            xSql = String.Format(xSql, xMasterTb, Integer.Parse(hdnCode.Value))

            dataAccess = GetDBAccess()

            Return dataAccess.ExecuteSqlString(xSql)

        Catch ex As Exception
            Return 0
        End Try
    End Function

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        DeleteMaster()
        GetMaster()
    End Sub

    Private Sub RadioMaster_CheckedChanged(sender As Object, e As EventArgs) _
                            Handles radCategory.CheckedChanged,
                                    RadManufactory.CheckedChanged
        GetMaster()
    End Sub

    Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
        Dim xSql As String
        Dim xMasterName As String
        Dim xMasterTb As String
        Dim xLinkName As String
        Dim da As DBAccess

        Try
            xMasterName = txtNew.Text.Trim()
            xLinkName = TrimAccentedString(xMasterName)
            xLinkName = TrimNonAlphaNumeric(xLinkName)
            xLinkName = TrimMultiWhiteSpace(xLinkName)
            xLinkName = xLinkName.Trim().ToLower()
            xLinkName = xLinkName.Replace(" ", "-")

            If xLinkName.Length = 0 Then
                Response.Write("<script type='text/javascript'>alert('Tên mà bạn đã nhập không hợp lệ. Xin hãy thử lại.');</script>")
                GetMaster()
                Return
            End If

            If radCategory.Checked Then
                xMasterTb = CommonConstants.T_CATEGORY
            Else
                xMasterTb = CommonConstants.T_MANUFACTORY
            End If

            xSql = String.Format("INSERT INTO {0} (NAME, LINK_NAME) VALUES ({1}, {2})", xMasterTb, ToSqlStr(xMasterName, SqlDataType.DataType_String), ToSqlStr(xLinkName, SqlDataType.DataType_String))

            da = GetDBAccess()

            da.ExecuteSqlString(xSql)

            Response.Write("<script type='text/javascript'>alert('Master đã được save thành công.');</script>")
            GetMaster()
            txtNew.Text = String.Empty
        Catch ex As Exception
            GetMaster()
            Response.Write("<script type='text/javascript'>alert('Đã xảy ra lỗi trong quá trình lưu master. Xin hãy thử lại!');</script>")
        End Try
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Dim xSql As String
        Dim xMasterName As String
        Dim xMasterTb As String
        Dim xLinkName As String
        Dim da As DBAccess

        Try
            xMasterName = hdnName.Value.Trim()
            xLinkName = TrimAccentedString(xMasterName)
            xLinkName = TrimNonAlphaNumeric(xLinkName)
            xLinkName = TrimMultiWhiteSpace(xLinkName)
            xLinkName = xLinkName.Trim().ToLower()
            xLinkName = xLinkName.Replace(" ", "-")

            If xLinkName.Length = 0 Then
                Response.Write("<script type='text/javascript'>alert('Tên mà bạn đã nhập không hợp lệ. Xin hãy thử lại.');</script>")
                GetMaster()
                Return
            End If

            If radCategory.Checked Then
                xMasterTb = CommonConstants.T_CATEGORY
            Else
                xMasterTb = CommonConstants.T_MANUFACTORY
            End If

            xSql = String.Format("UPDATE {0} SET NAME = {1}, LINK_NAME = {2} WHERE CODE={3}", xMasterTb, ToSqlStr(xMasterName, SqlDataType.DataType_String), ToSqlStr(xLinkName, SqlDataType.DataType_String), ToSqlStr(hdnCode.Value, SqlDataType.DataType_Integer))

            da = GetDBAccess()

            da.ExecuteSqlString(xSql)

            Response.Write("<script type='text/javascript'>alert('Master đã được save thành công.');</script>")
            GetMaster()
            hdnCode.Value = String.Empty
            hdnName.Value = String.Empty
        Catch ex As Exception
            GetMaster()
            Response.Write("<script type='text/javascript'>alert('Đã xảy ra lỗi trong quá trình lưu master. Xin hãy thử lại!');</script>")
        End Try
    End Sub
End Class